//
//  CellThree.m
//  project-A (pianke)
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 scott. All rights reserved.
//

#import "CellThree.h"
#import "Cell2ofscroll.h"

#import "CellTwoTab.h"
#import "CellTwoTab2.h"
#import "Model2.h"
#import <UIImageView+WebCache.h>
#import "NetworkingHandle.h"
#import "WebviewControl.h"
#import <DKNightVersion.h>
#import <MJRefresh.h>

#define  WIDTH self.frame.size.width

#define HEIGHT self.frame.size.height


@interface CellThree ()<UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate>

@property (nonatomic,retain)NSMutableArray *arr;

@property (nonatomic,retain)NSMutableArray *picarr;

@property (nonatomic,assign)NSInteger number;

@end

@implementation CellThree

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
  
        self.number = 2;
        self.tabview = [[UITableView alloc]init];
        
        [self.contentView addSubview:self.tabview];
        
        self.tabview.backgroundColor = [UIColor whiteColor];
        
        self.tabview.delegate = self;
        self.tabview.dataSource = self;
        self.tabview.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
        
        self.tabview.separatorStyle =UITableViewCellSeparatorStyleNone;
        
        [self.tabview registerClass:[CellTwoTab class] forCellReuseIdentifier:@"pool1"];
        [self.tabview registerClass:[CellTwoTab2 class] forCellReuseIdentifier:@"pool2"];
        [self.tabview registerClass:[Cell2ofscroll class] forCellReuseIdentifier:@"pool0"];
         self.arr = [NSMutableArray array];
#pragma mark 下拉刷新
        
        NSMutableArray *marray2 = [NSMutableArray array];
        for (int i = 1; i < 25; i++) {
            UIImage *image2 = [UIImage imageNamed:[NSString stringWithFormat:@"Bzombie%d.tiff",i]];
            [marray2 addObject:image2];
        }

        
        MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewdata)];
        [header setImages:marray2 forState:MJRefreshStateIdle];
        // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
        [header setImages:marray2 forState:MJRefreshStatePulling];
        // 设置正在刷新状态的动画图片
        [header setImages:marray2 forState:MJRefreshStateRefreshing];
        
        
        // 设置header
        self.tabview.mj_header = header;
        
#pragma mark 上拉刷新
        self.tabview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
           
            NSLog(@"上拉刷新");
            
            NSString *str = [NSString stringWithFormat:@"http://lol.zhangyoubao.com/apis/rest/ItemsService/lists?catid=10178&cattype=news&page=%ld&i_=868299012353735&t_=1453899132647&p_=10286&v_=400609&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=15&cha=tencentMartket&u_=&",self.number++];
            
          
            
            
            [NetworkingHandle handleWorkData:str AndBody:nil value:nil handleBlock:^(id result) {
                
                self.picarr = [NSMutableArray array];
               
                
                NSMutableArray *arrshuju = [result objectForKey:@"data"];
                for (NSDictionary *dic in arrshuju) {
                    
                    Model2 *model = [[Model2 alloc]init];
                    [model setValuesForKeysWithDictionary:dic];
                    NSString *str  =   [dic objectForKey:@"id"];
                    model.idd = str;
                    [self.arr addObject:model];
                    
                    
                    
                    [model release];
                }
//                NSLog(@"%ld",self.arr.count);
//                
//                NSLog(@"%@",self.arr);
                
                
                [self.tabview.mj_footer endRefreshing];
                  [self.tabview reloadData];
            }];

            
        }];
        
        [self.tabview release];
    }
    
    return self;
}

-(void)dealloc{
    [_tabview release];
    [_arr release];
    [_picarr release];
    [super dealloc];
    
}

#pragma mark 下拉刷新的方法

-(void)loadNewdata{
    
    NSLog(@"下拉刷新");
    
    [self.tabview.mj_header endRefreshing];

    
}



#pragma mark 协议方法tabview




-(void)geturl{
 NSString *str = @"http://lol.zhangyoubao.com/apis/rest/ItemsService/lists?catid=10178&cattype=news&page=1&i_=868299012353735&t_=1453899132647&p_=10286&v_=400609&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=15&cha=tencentMartket&u_=&";
    
    [NetworkingHandle handleWorkData:str AndBody:nil value:nil handleBlock:^(id result) {
        self.arr = [NSMutableArray array];
        self.picarr = [NSMutableArray array];
        
        NSMutableArray *arrshuju = [result objectForKey:@"data"];
        for (NSDictionary *dic in arrshuju) {
            
            Model2 *model = [[Model2 alloc]init];
            [model setValuesForKeysWithDictionary:dic];
        NSString *str  =   [dic objectForKey:@"id"];
            model.idd = str;
            [self.arr addObject:model];
            [model release];
//            NSLog(@"%@",model.idd);
        }
       
       
        [self.tabview reloadData];
       
        
    }];
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arr.count + 1;

    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return WIDTH * 0.4;
    }else{
    return WIDTH * 0.225 + 30;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        Cell2ofscroll *cell = [tableView dequeueReusableCellWithIdentifier:@"pool0"];
        
        return cell;
    }
        Model2 *model = [self.arr objectAtIndex:indexPath.row - 1];
        if ([model.pic_url isEqualToString:@""]) {
            CellTwoTab *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
            
            cell.title.text = model.title;
            cell.desc.text = model.desc;
            
           
            cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],  [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
            
            NSString *str = model.published;
            
            NSTimeInterval time=[str doubleValue];
            NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
                       //实例化一个NSDateFormatter对象
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            //设定时间格式,这里可以设置成自己需要的格式
            [dateFormatter setDateFormat:@"MM-dd HH:mm"];
            
            NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
          
            cell.time.text =  currentDateStr;
            
            if (![model.weight_new isEqualToString:@"0"]) {
             
                cell.image.hidden = NO;
                
                
                
            }
            
                    return cell;
            }else{
        
            CellTwoTab2 *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
            
            cell.title.text = model.title;
            cell.desc.text = model.desc;
                
            NSString *str = model.published;
            NSTimeInterval time=[str doubleValue];
            NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
            NSLog(@"date:%@",[detaildate description]);
            //实例化一个NSDateFormatter对象
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            //设定时间格式,这里可以设置成自己需要的格式
            [dateFormatter setDateFormat:@"MM-dd HH:mm"];
            
            NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
            
           
            cell.time.text =  currentDateStr;
            
             [cell.image sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:nil options:SDWebImageAvoidAutoSetImage];
            
//            if ([model.weight_new isEqualToString:@"0"]) {
//                
//                cell.image3.hidden = YES;
//            }
                
                cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor],  [UIColor colorWithRed:0.16 green:0.14 blue:0.13 alpha:1]);
                
            return cell;
    }
}





#pragma mark 协议方法


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
   
    
    [self.delegate didClickWith:[self.arr objectAtIndex:indexPath.row - 1]];
    
    
}

-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    
    self.tabview.frame = self.contentView.frame;
    
}

-(UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    
    return  layoutAttributes;
    
}
-(void)layoutSubviews{
    
    [super layoutSubviews];
}

@end
